package leetcode.code0609;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import leetcode.IDebug;
import leetcode.helper.H;

public class Solution implements IDebug {

	public List<List<String>> findDuplicate(String[] paths) {
		Map<String, List<String>> map = new HashMap<>();
		for (String path : paths) {
			String[] p = path.split(" ");
			String dic = "/";
			int s = 0;
			if (p[0].indexOf(".") == -1) {
				dic = p[0] + "/";
				s = 1;
			}
			for (int i = s; i < p.length; i++) {
				int dot = p[i].indexOf(".txt");
				String content = p[i].substring(dot + 5, p[i].length() - 1);
				map.computeIfAbsent(content, key -> new ArrayList<>()).add(dic + p[i].substring(0, dot + 4));
			}
		}
		List<List<String>> ans = new ArrayList<>();
		for (Map.Entry<String, List<String>> entry : map.entrySet()) {
			if (entry.getValue().size() > 1) {
				ans.add(entry.getValue());
			}
		}
		return ans;
	}

	@Override
	public void debug4() {
		this.findDuplicate(H.str2array2(
				"[\"root/a 1.txt(abcd) 2.txt(efgh)\",\"root/c 3.txt(abcd)\",\"root/c/d 4.txt(efgh)\",\"root 4.txt(efgh)\"]"));

	}

	@Override
	public void debug3() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug2() {
		// TODO Auto-generated method stub

	}

	@Override
	public void debug1() {
		// TODO Auto-generated method stub

	}

	public static void main(String[] args) {
		Solution so = new Solution();
		so.debug1();
		so.debug2();
		so.debug3();
		so.debug4();
		System.out.println("3.txt(abcd)".indexOf(".txt"));

	}

}
